package com.rxjavademo.base;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;

import com.rxjavademo.base.BaseView;
import com.rxjavademo.base.BasePresenter;

import butterknife.ButterKnife;

/**
 * Created by robert.luozizhao on 2018/1/17.
 */

public abstract class BaseActivity<P extends BasePresenter> extends AppCompatActivity implements BaseView{

    protected P presenter;
    public Context context;
    private int layout;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayout());
        ButterKnife.bind(this);
        initView();
        context = this;
        presenter = initPresenter();
    }

    /**
     * 在子类中初始化对应的presenter
     * @return
     */
    public abstract P initPresenter();

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }


    /**
     * 某个activity 销毁时防止内存泄露，解绑presenter
     */
    @Override
    protected void onDestroy() {
        if (presenter != null) {
            presenter.detachView();//在presenter中解绑释放view
            presenter = null;
        }
        ButterKnife.unbind(this);
        super.onDestroy();
    }

    protected abstract int getLayout();
    protected abstract void initView();

}
